Trò chơi Tic-Tac-Toe, game đánh caro full source code
- SocketUdp.cs
- PhotonNetwork /
- Plugins /
- Photon Unity Networking /
- Assets /
- project /
2 // <copyright file="CSharpSocket.cs" company="Exit Games GmbH">
3 // Protocol & Photon Client Lib - Copyright (C) 2013 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 // Uses the UDP socket for a peer to send and receive enet/Photon messages.
7 // </summary>
8 // <author>developer@exitgames.com</author>
9 // --------------------------------------------------------------------------------------------------------------------
10
11 #if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_IPHONE && !UNITY_PS3 && !UNITY_WINRT && !UNITY_WP8)
12
13 namespace ExitGames.Client.Photon
14 {
15 using System;
16 using System.Net;
17 using System.Net.Sockets;
18 using System.Security;
19 using System.Threading;
20
21 /// <summary> Internal class to encapsulate the network i/o functionality for the realtime libary.</summary>
22 internal class SocketUdp : IPhotonSocket
23 {
24 private Socket sock;
25
26 private readonly object syncer = new object();
27
28 public SocketUdp(PeerBase npeer) : base(npeer)
29 {
30 if (this.ReportDebugOfLevel(DebugLevel.ALL))
31 {
32 this.Listener.DebugReturn(DebugLevel.ALL, "CSharpSocket: UDP, Unity3d.");
33 }
34
35 this.Protocol = ConnectionProtocol.Udp;
36 this.PollReceive = false;
37 }
38
39 public override bool Connect()
40 {
41 lock (this.syncer)
42 {
43 bool baseOk = base.Connect();
44 if (!baseOk)
45 {
46 return false;
47 }
48
49 this.State = PhotonSocketState.Connecting;
50
51 Thread dns = new Thread(this.DnsAndConnect);
52 dns.Name = "photon dns thread";
53 dns.IsBackground = true;
54 dns.Start();
55
56 return true;
57 }
58 }
59
60 public override bool Disconnect()
61 {
62 if (this.ReportDebugOfLevel(DebugLevel.INFO))
63 {
64 this.EnqueueDebugReturn(DebugLevel.INFO, "CSharpSocket.Disconnect()");
65 }
66
67 this.State = PhotonSocketState.Disconnecting;
68
69 lock (this.syncer)
70 {
71 if (this.sock != null)
72 {
73 try
74 {
75 this.sock.Close();
76 }
77 catch (Exception ex)
78 {
79 this.EnqueueDebugReturn(DebugLevel.INFO, "Exception in Disconnect(): " + ex);
80 }
81
82 this.sock = null;
83 }
84 }
85
86 this.State = PhotonSocketState.Disconnected;
87 return true;
88 }
89
90 /// <summary>used by PhotonPeer*</summary>
91 public override PhotonSocketError Send(byte[] data, int length)
92 {
93 lock (this.syncer)
94 {
95 if (!this.sock.Connected)
96 {
97 return PhotonSocketError.Skipped;
98 }
99
100 try
101 {
102 sock.Send(data, 0, length, SocketFlags.None);
103 }
104 catch
105 {
106 return PhotonSocketError.Exception;
107 }
108 }
109
110 return PhotonSocketError.Success;
111 }
112
113 public override PhotonSocketError Receive(out byte[] data)
114 {
115 data = null;
116 return PhotonSocketError.NoData;
117 }
118
119 internal void DnsAndConnect()
120 {
121 try
122 {
123 lock (this.syncer)
124 {
125 this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
126
127 IPAddress ep = IPhotonSocket.GetIpAddress(this.ServerAddress);
128 this.sock.Connect(ep, this.ServerPort);
129
130 this.State = PhotonSocketState.Connected;
131 }
132 }
133 catch (SecurityException se)
134 {
135 if (this.ReportDebugOfLevel(DebugLevel.ERROR))
136 {
137 this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
138 }
139
140 this.HandleException(StatusCode.SecurityExceptionOnConnect);
141 return;
142 }
143 catch (Exception se)
144 {
145 if (this.ReportDebugOfLevel(DebugLevel.ERROR))
146 {
147 this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
148 }
149
150 this.HandleException(StatusCode.ExceptionOnConnect);
151 return;
152 }
153
154 Thread run = new Thread(new ThreadStart(ReceiveLoop));
155 run.Name = "photon receive thread";
156 run.IsBackground = true;
157 run.Start();
158 }
159
160 /// <summary>Endless loop, run in Receive Thread.</summary>
161 public void ReceiveLoop()
162 {
163 byte[] inBuffer = new byte[this.MTU];
164 while (this.State == PhotonSocketState.Connected)
165 {
166 try
167 {
168 int read = this.sock.Receive(inBuffer);
169 this.HandleReceivedDatagram(inBuffer, read, true);
170 }
171 catch (Exception e)
172 {
173 if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected)
174 {
175 if (this.ReportDebugOfLevel(DebugLevel.ERROR))
176 {
177 this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + " Exception: " + e);
178 }
179
180 this.HandleException(StatusCode.ExceptionOnReceive);
181 }
182 }
183 } //while Connected receive
184
185 // on exit of the receive-loop: disconnect socket
186 this.Disconnect();
187 }
188 } //class
189
190 }
191 #endif
--------------------------------------------------------------------------------------------------------------------
Protocol & Photon Client Lib - Copyright (C) 2013 Exit Games GmbH
Uses the UDP socket for a peer to send and receive enetPhoton messages.
--------------------------------------------------------------------------------------------------------------------
} while Connected receive
on exit of the receive-loop: disconnect socket
} class